package net.zjitc.controller;

import net.zjitc.pojo.Category;
import net.zjitc.pojo.Notice;
import net.zjitc.pojo.User;
import net.zjitc.service.CategoryService;
import net.zjitc.service.NoticeService;
import net.zjitc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/3/27 15:02
 */
@Controller
public class loginController {
    @Autowired
    UserService userService;
    @RequestMapping(value = "/loginUI")
    public String loginUI(){
        return "/client/login.jsp";
    }

    @RequestMapping(value = "/login")
    public String login(Model model,User user,HttpSession session){
        User u =  userService.login(user.getUsername(),user.getPassword());
        if (u != null)
        {
            session.setAttribute("user",u);
            return "/index.jsp";
        }
        model.addAttribute("news","用户名或密码错误");
        return "/client/login.jsp";
    }

    @RequestMapping(value = "/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "/index.jsp";
    }

    @RequestMapping(value = "/admin_login")
    public String admin_login(Model model,User user,HttpSession session){
        User u =  userService.login(user.getUsername(),user.getPassword());
        if (u != null)
        {
            if (!"超级用户".equals(u.getRole())){
                model.addAttribute("news","你不是管理员，没有权限登录");
                return "/admin/login/login.jsp";
            }else {
                session.setAttribute("user",u);
                return "/admin/login/home.jsp";
            }
        }
        model.addAttribute("news","用户名或密码错误");
        return "/admin/login/login.jsp";
    }
}
